<?php
    
    class Ad extends Admin{
    	
		public function __construct(){
			parent::__construct();
			
		}
		
		
		public function Index(){
			$this->mod = "Index";
			if(!$_SESSION['U']['adminid']){
				$this->showmessage("您还未登录","?m=Admin/Login" ,2000, "用户登录");
			}else{
				$this->_T['U'] = $_SESSION['U'];
				$this->template();
			}
		}
		public function setPlugIn(){   //加载第三方插件，此函数将被自动执行
			return array("page.php","editor.php");
		}
		

		/*广告列表*/
		public function AdList(){
			$this->mod = "AdList";
			$sql="SELECT * FROM `bb_ad` AS a  JOIN `bb_ad_info` AS b WHERE (a.adid=b.adid)";
			$page = new Page(10,"$sql");
			$this->_T['function'] ="AdList";
			$this->_T['AdList']['con'] = $page->content;  //二维数组
			$this->_T['AdList']['Num'] = $page->num;
			$this->_T['AdList']['pageOne']  = $page->first_page;
			$this->_T['AdList']['pageLast'] = $page->last_page;
			$this->_T['AdList']['pageVal']  = $page->pageval;
			$this->_T['AdList']['pagePre']  = $page->pageval-1;
			$this->_T['AdList']['pageNext'] = $page->pageval+1;
			$this->template("parent");
		}
		
		/*创建广告*/
		function create()
	{
		$ad = $_POST['ad'];
		$ad['start_time'] = $ad['start_time'] ? strtotime($ad['start_time']) : 0;
		$ad['end_time'] = $ad['end_time'] ? strtotime($ad['end_time']) : 0;
		$adid = DB::insert("bb_ad", $ad);
		$ad_info = $_POST['ad_info'];
		$ad_info['adid'] = $adid;
		DB::insert('bb_ad_info', $ad_info);
		
		if ($ad['type'] == 'image')
		{
			$image_image = $_POST['image_image'];
			$image_image['adid'] = $adid;
			if ($_FILES['image_image_file']['name'])
			{				
				$objectid = File::uploadFile('image_image_file','image','storage');
				if ($objectid>0)
				{
					$image_image['width'] = $image_image['width']?$image_image['width']:0;	
					$image_image['height'] = $image_image['height']?$image_image['height']:0;
					$image_image['objectid'] = $objectid;
					$file=DB::fetch_row("SELECT * FROM `bb_file` WHERE `objectid`='{$objectid}'");
					$image_image['src']=$file['realname'];
				}
				else
				{
					$this->showmessage("创建失败", "goback", 2000 , "增加广告");
				}
			}
			DB::insert('bb_ad_image', $image_image);
		}
		else if ($ad['type'] == 'slide')
		{
			foreach($_POST['slide_image_order'] AS $key => $slide_order)
			{
				$slide_image = array();
				$slide_image['adid'] = $adid;
				$slide_image['src'] = $_POST['slide_image_src'][$key];
				$slide_image['url'] = $_POST['slide_image_url'][$key];
				$slide_image['alt'] = $_POST['slide_image_alt'][$key];
				$slide_image['title'] = $_POST['slide_image_title'][$key];
				if ($_FILES['slide_image_file'.$slide_order]['name'])
				{				
					$objectid = File::uploadFile('slide_image_file'.$slide_order,'image','storage');
					$file=DB::fetch_row("SELECT * FROM `bb_file` WHERE `objectid`='{$objectid}'");
					$slide_image['src']=$file['realname'];
					if ($objectid>0)
						$slide_image['objectid'] = $objectid;
					else
						$this->showmessage("创建失败", "goback", 2000 , "增加广告");
				
				DB::insert('bb_ad_image', $slide_image);
				}
				
			}
		}
		$this->showmessage("创建成功", "?m=Admin/Ad/AdList", 2000 , "广告列表");
	}


		/*编辑广告*/
		function EditAd()
	{
		$this->mod = "AdEdit";
		$adid  = $_GET['adid']?$_GET['adid']:$_POST['adid'];
		$ad = DB::fetch_row("SELECT * FROM `bb_ad` WHERE `adid`='{$adid}'");
		if(!$ad)
			$this->showmessage("创建成功", "goback", 2000 , "广告列表");	
		$ad_info = DB::fetch_row("SELECT * FROM `bb_ad_info` WHERE `adid`='{$adid}'");
		
		
		if ($ad['type'] == 'image')
		{
			$image_image = DB::fetch_row("SELECT * FROM `bb_ad_image` AS ai JOIN `bb_file` AS f WHERE ai.objectid = f.objectid AND ai.adid = '{$adid}'");
			
		}
		else if ($ad['type'] == 'slide')
		{
			$slide_images = array();
			$sql = "SELECT * FROM `bb_ad_image`  AS ai JOIN `bb_file` AS f  WHERE ai.objectid = f.objectid AND ai.adid = '{$adid}'";
			$slide_images = DB::fetch_array($sql);
		}
		
		if ($ad['start_time'])
			$ad['start_time'] = date("Y-m-d H:i:s", $ad['start_time']);
		if ($ad['end_time'])
			$ad['end_time'] = date("Y-m-d H:i:s", $ad['end_time']);
		
		$ad_info['description'] = htmlspecialchars($ad_info['description']);
		
		$this->_T['adid'] = $adid;
		$this->_T['ad'] = $ad;
		$this->_T['ad_info'] = $ad_info;
		$this->_T['image_image'] = $image_image;
		$this->_T['slide_images'] = $slide_images;
		$this->_T['ifnone'] = $ifnone;
		
		$this->template("parent");
	}
		
		/*更新广告*/
		
		function Update()
	{
		$adid  = $_POST['adid'];
		$oldad = DB::fetch_row("SELECT * FROM `bb_ad` WHERE adid = '{$adid}'");
		if (!$oldad)
			$this->showmessage("创建失败", "goback", 2000 , "广告列表");	
		
		$ad = $_POST['ad'];	
		$ad['start_time'] = $ad['start_time'] ? strtotime($ad['start_time']) : 0;
		$ad['end_time'] = $ad['end_time'] ? strtotime($ad['end_time']) : 0;
		
		$conditions = array();
		$conditions['adid'] = $adid;
		$ad_id = array();
		$ad_id['adid'] = $adid;
		DB::update("bb_ad", $ad,$conditions);
		$ad_info = $_POST['ad_info'];
		DB::update("bb_ad_info", $ad_info,$conditions);
		if ($ad['type'] == 'image')
		{
			$oldimage = DB::fetch_row("SELECT * FROM `bb_ad_image` WHERE adid = '{$adid}'");			
			$conditions['id'] = $oldimage['id'];			
			$image_image = $_POST['image_image'];
			
			if ($_FILES['image_image_file']['name'])
			{
				$condition = array();
				$condition['objectid'] = $oldimage['objectid'];
				DB::delete('bb_file', $condition);
				File::deleteFile($oldimage['src'],"delete file");
				$objectid = File::uploadFile('image_image_file','image','storage');					
				if ($objectid>0)
				{
							
					$image_image['width'] = $image_image['width']?$image_image['width']:0;	
					$image_image['height'] = $image_image['height']?$image_image['height']:0;
					$image_image['objectid'] = $objectid;
					$file=DB::fetch_row("SELECT * FROM `bb_file` WHERE `objectid`='{$objectid}'");
					$image_image['src']=$file['realname'];
				}
				else
				{
					$this->showmessage("创建失败", "goback", 2000 , "广告列表");
				}
			}
			DB::update('bb_ad_image', $image_image,$ad_id);
		}
		else if ($ad['type'] == 'slide')
		{
			$old_slide_images= array();
			$old_slide_images=DB::fetch_array("SELECT * FROM `bb_ad_image` WHERE adid = '{$adid}'");
			
			$morepicnum = $_POST['morepicnum'];

			for($i = 0; $i < $morepicnum; $i++)//原有图片处理
			{
				
				$slide_image_id = $_POST['slide_image_id'][$i];
				if (in_array($slide_image_id, $_POST['slide_image_destroy']))//如果被勾选了删除
				{
					$con = array();
					$con['id'] = $slide_image_id;
					$options = array();
					$options['objectid'] = $old_slide_images[$i]['objectid'];
					
					$file=DB::fetch_row("SELECT * FROM `bb_ad_image` WHERE `id`='{$slide_image_id}'");
					$objectid = $file['objectid'];
					$option = DB::fetch_row("SELECT * FROM `bb_file` WHERE `objectid`='{$objectid}'");
					$realname = $option['realname'];
					File::deleteFile("$realname","delete file");
					DB::delete('bb_file', $options);
					DB::delete("bb_ad_image", $con);
					continue;
				}
				
				$slide_image = array();
				$slide_image['src'] = $_POST['slide_image_src'][$i];
				$slide_image['url'] = $_POST['slide_image_url'][$i];
				$slide_image['alt'] = $_POST['slide_image_alt'][$i];
				$slide_image['title'] = $_POST['slide_image_title'][$i];
				$kk=$i+1;
				if ($_FILES['slide_image_file'.$kk]['name'])
				{
					
					File::deleteFile($old_slide_images[$i]['src'],"delete file");
					if($old_slide_images[$i]['objectid']){
						$options = array();
						$options['objectid'] = $old_slide_images[$i]['objectid'];
						DB::delete('bb_file', $options);
 						$objectid = File::uploadFile('slide_image_file'.$kk,'image','storage');	
					}		
					if ($objectid>0)
					{	
						$slide_image['width'] = $slide_image['width']?$slide_image['width']:0;	
						$slide_image['height'] = $slide_image['height']?$slide_image['height']:0;
						$slide_image['objectid'] = $objectid;
						$file=DB::fetch_row("SELECT * FROM `bb_file` WHERE `objectid`='{$objectid}'");
						$slide_image['src']=$file['realname'];
						
					}
					else
					{
						$this->showmessage("更新失败", "goback", 2000 , "广告列表");
					}						
				}
				$conditions=array();
				$conditions['id'] = $slide_image_id;
				DB::update('bb_ad_image', $slide_image,$conditions);
			}
			
			for($k=$morepicnum,$morepicnum++,$i = $morepicnum; $i <= count($_POST['slide_image_order']); $i++)//新增部分处理
			{
				$slide_image = array();
				$slide_image['adid'] = $adid;
				$slide_image['src'] = $_POST['slide_image_src'][$k];
				$slide_image['url'] = $_POST['slide_image_url'][$k];
				$slide_image['alt'] = $_POST['slide_image_alt'][$k];
				$slide_image['title'] = $_POST['slide_image_title'][$k];
				if ($_FILES['slide_image_file'.$i]['name'])
				{
					$options = array();
					$options['key'] = $i;
					$options['description'] = $slide_image['alt'];
					$options['adid'] = $adid;					
					$objectid =File::uploadFile('slide_image_file'.$i,'image','storage');
					$slide_image['objectid'] = $objectid;									
					if ($objectid>0)
					{	
						$slide_image['width'] =$slide_image['width']?$slide_image['width']:0;	
						$slide_image['height'] = $slide_image['height']?$slide_image['height']:0;
						$file=DB::fetch_row("SELECT * FROM `bb_file` WHERE `objectid`='{$objectid}'");
						$slide_image['src']=$file['realname'];
					}
					else
					{
						$this->showmessage("更新失败", "goback", 2000 , "广告列表");
					}
				}
				 DB::insert('bb_ad_image', $slide_image);
			}
		}
		$this->showmessage("更新成功", "?m=Admin/Ad/AdList", 2000 , "广告列表");
	}
		
		/*删除广告*/
		function DeletiAd(){
		$adid  = $_GET['adid']?$_GET['adid']:$_POST['adid'];
		$ad = DB::fetch_row("SELECT * FROM `bb_ad` WHERE adid = '{$adid}'");
		if (!$ad)
			$this->showmessage("广告不存在", "?m=Admin/Ad/AdList", 2000 , "广告列表");
		$condition = array();
		$condition['adid'] = $adid;

		if(DB::delete('bb_ad', $condition))
		{
			DB::delete('bb_ad_info', $condition);
			$sql = "SELECT * FROM `bb_ad_image` WHERE adid = '{$adid}'";
			$num = DB::num_rows($sql);
			while($num--)
			{
				$ad_image = DB::fetch_row($sql);						
				$filecondition = $imagecondition  = array();
				$filecondition['objectid'] = $ad_image['objectid'];				
				$imagecondition['id'] = $ad_image['id'];
				File::deleteFile($ad_image['src'],"delete file");
				DB::delete('bb_file', $filecondition);
				DB::delete('bb_ad_image', $imagecondition);
			}
			
			$this->showmessage("删除成功", "?m=Admin/Ad/AdList", 2000 , "广告列表");
		};
		$this->showmessage("删除失败", "?m=Admin/Ad/AdList", 2000 , "广告列表");	
		
	}

	
		public function AddAd(){
			$this->mod = "AdAdd";
			$this->template("parent");
		}
			
		
		public function Out(){
			$_SESSION['U'] = null;
			$this->showmessage("安全退出","?m=Admin/Login" ,2000, "返回登陆页");
		}
    }




?>